/**********************************************************************
*  Copyright (c) 2007 - Victor Jacobs - victor.jacobs@gmail.com
*
*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
*
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Vj.Controls
{
    public partial class RotationControl : UserControl
    {
        private Graphics g;
        private Point center;
        private Point outer;
        Pen pen;

        public RotationControl()
        {
            InitializeComponent();
            g = this.CreateGraphics();
            pen = new Pen(Color.Black, 1);
            this.RotationControl_Resize(this, null);
        }

        private bool drawBorder = false;

        public bool DrawBorder
        {
            get { return drawBorder; }
            set { drawBorder = value; }
        }

        private bool drawX = false;

        public bool DrawX
        {
            get { return drawX; }
            set { drawX = value; }
        }

        private double angle = 0.0;

        public double Angle
        {
            get { return angle; }
            //set { angle = value; }
        }



        private void RotationControl_Paint(object sender, PaintEventArgs e)
        {
            if (drawX)
            {
                g.DrawLine(pen, 0, 0, Width - 1, Height - 1);
                g.DrawLine(pen, Width - 1, 0, 0, Height - 1);
            }

            if (drawBorder)
            {
                g.DrawLine(pen, 0, 0, Width - 1, 0);
                g.DrawLine(pen, Width - 1, 0, Width - 1, Height - 1);
                g.DrawLine(pen, Width - 1, Height - 1, 0, Height - 1);
                g.DrawLine(pen, 0, Height - 1, 0, 0);
            }

            g.FillEllipse(Brushes.LightGray, 1, 1, this.Width - 2, this.Height - 2);
            g.DrawEllipse(pen, 1, 1, this.Width - 2, this.Height - 2);
            g.DrawLine(pen, center.X, center.Y, outer.X, outer.Y);
        }

        private void RotationControl_Resize(object sender, EventArgs e)
        {
            if (this.Width % 2 != 0)
                this.Width -= 1;

            if (this.Height % 2 != 0)
                this.Height -= 1;

            center = new Point(this.Width / 2, this.Height / 2);
            outer = new Point(this.Width / 2, 2);
        }

        private void RotationControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                UpdateLine(e);
            }
        }

        private void RotationControl_MouseDown(object sender, MouseEventArgs e)
        {
            UpdateLine(e);
        }


        private void UpdateLine(MouseEventArgs e)
        {
            Point pt1 = new Point();
            pt1.X = e.Location.X - center.X;
            pt1.Y = e.Location.Y - center.Y;

            int R = this.Width / 2 - 2;  // radius
            double mag = Math.Sqrt(pt1.X * pt1.X + pt1.Y * pt1.Y);
            double k = R / mag;

            Point pt2 = new Point();

            pt2.X = center.X + (int)(k * pt1.X);
            pt2.Y = center.Y + (int)(k * pt1.Y);

            outer = new Point(pt2.X, pt2.Y);

            angle = Math.Asin((double)e.X / (double)R);

            this.Refresh();
        
        }

    }
}
